﻿using Entitas;
using UnityEngine;

namespace Script.Systems
{
    public class EnemySpawnSystem : IExecuteSystem
    {
        private float    timer = 1.5f;
        private Contexts myContexts;

        public EnemySpawnSystem (Contexts contexts)
        {
            myContexts = contexts;
        }
        
        public void Execute ()
        {
            var dt = Time.deltaTime;
            if (timer>0)
            {
                timer -= dt;
            }
            else
            {
                timer = 1.5f;
                var x = Random.Range (-9f, 9f);
                var y = Random.Range (-5f, 5f);
                var enemyEntity = EntityUtils.CreateEnemyEntity (myContexts, new Vector2 (x,y), Vector2.zero, 0);
                
                //give enemy targetId
                var playerEntity = myContexts.game.playerTagEntity;
                enemyEntity.AddTargetComp (playerEntity.id.value);
            }
        }
    }
}